using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;

/// <summary>
/// Summary description for LocationDAO
/// </summary>
public class LocationDAO:DBAccess
{
	#region sql Commands   
    string SQL_SELECT_ALL = "select * from Location";
    string SQL_SELECT_BY_ID = "select * from Location where ID=@id";
    string SQL_INSERT = "insert into Location(Name,Description,ImageURL,Article,Tag,State,ModifiedDate,ModifiedBy)values(@Name,@Description,@ImageURL,@Article,@Tag,@State,@ModifiedDate,@ModifiedBy)";
    string SQL_UPDATE = "update Location set Name = @Name,Description= @Description,ImageURL=@ImageURL ,Article=@Article,Tag=@Tag,State=@State ,ModifiedDate=@ModifiedDate,ModifiedBy=@ModifiedBy where ID = @id";
    string SQL_DELETE = "delete from Location where ID = @id";
    #endregion

    #region fields
    string message = "";
    #endregion

    #region constructors
    public LocationDAO()
    {

    }
    #endregion

    #region properties
    public string Message
    {
        get { return message; }
        set { message = value; }
    }
    #endregion

    #region Methos
    /// <summary>
    /// Get location by id
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public Location GetLocation(int id)
    {
        Location loca = new Location();
        try
        {

            SqlCommand command = new SqlCommand(SQL_SELECT_BY_ID, conn);
            if (Connect())
            {
                command.Parameters.AddWithValue("@id", id);
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    loca.Id = dr.GetInt32(0);
                    loca.Name = dr.GetString(1);                                  
                    loca.Description = dr.GetString(2);
                    loca.ImageURL = dr.GetString(3);                   
                    loca.Article = dr.GetString(4);                   
                    loca.Tag = dr.GetString(5);
                    loca.State = dr.GetBoolean(6);
                    loca.ModifiedDate = dr.GetDateTime(7);
                    loca.ModifiedBy = dr.GetInt32(8);
                   
                }
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return loca;
    }
    /// <summary>
    /// Get all locations 
    /// </summary>
    /// <returns></returns>
    public IList<Location> GetAllLocations()
    {
        IList<Location> list = new List<Location>();
        try
        {
            SqlCommand command = new SqlCommand(SQL_SELECT_ALL, conn);
            if (Connect())
            {
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    Location loca = new Location();
                    loca.Id = dr.GetInt32(0);
                    loca.Name = dr.GetString(1);                                  
                    loca.Description = dr.GetString(2);
                    loca.ImageURL = dr.GetString(3);                   
                    loca.Article = dr.GetString(4);                   
                    loca.Tag = dr.GetString(5);
                    loca.State = dr.GetBoolean(6);
                    loca.ModifiedDate = dr.GetDateTime(7);
                    loca.ModifiedBy = dr.GetInt32(8);
                    list.Add(loca);
                }
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return list;
    }
    /// <summary>
    /// Insert new location into database
    /// </summary>
    /// <param name="tour"></param>
    /// <returns></returns>
    public int Insert(Location loc)
    {
        int effectedRows = 0;
        try
        {
            SqlCommand command = new SqlCommand(SQL_INSERT, conn);
            command.Parameters.AddWithValue("@Name", loc.Name);
            command.Parameters.AddWithValue("@Description", loc.Description);
            command.Parameters.AddWithValue("@ImageURL", loc.ImageURL);
            command.Parameters.AddWithValue("@Article", loc.Article);
            command.Parameters.AddWithValue("@Tag", loc.Tag);
            command.Parameters.AddWithValue("@State", loc.State);
            command.Parameters.AddWithValue("@ModifiedDate", loc.ModifiedDate);
            command.Parameters.AddWithValue("@ModifiedBy", loc.ModifiedBy);
            
            if (Connect())
            {
                effectedRows = command.ExecuteNonQuery();
                command.CommandText = "SELECT @@IDENTITY FROM Location";
                loc.Id = int.Parse(command.ExecuteScalar().ToString());
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRows;
    }

    /// <summary>
    /// Update location
    /// </summary>
    /// <param name="tour"></param>
    /// <returns></returns>
    public int Update(Location loca)
    {
        int effectedRows = 0;
        try
        {
            SqlCommand command = new SqlCommand(SQL_UPDATE, conn);
            command.Parameters.AddWithValue("@Name", loca.Name);
            command.Parameters.AddWithValue("@Description", loca.Description);
            command.Parameters.AddWithValue("@ImageURL", loca.ImageURL);
            command.Parameters.AddWithValue("@Article", loca.Article);
            command.Parameters.AddWithValue("@Tag", loca.Tag);
            command.Parameters.AddWithValue("@State", loca.State);
            command.Parameters.AddWithValue("@ModifiedDate", loca.ModifiedDate);
            command.Parameters.AddWithValue("@ModifiedBy", loca.ModifiedBy);            ;
            command.Parameters.AddWithValue("@id",loca.Id);  
            if (Connect())
            {
                effectedRows = command.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRows;
    }

    /// <summary>
    /// Remove location out of database
    /// </summary>
    /// <param name="tour"></param>
    /// <returns></returns>
    public int Delete(int  Id)
    {
        int effectedRows = 0;
        try
        {
            SqlCommand command = new SqlCommand(SQL_DELETE, conn);
            command.Parameters.AddWithValue("@id", Id);
            if (Connect())
            {
                effectedRows = command.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRows;
    }
    #endregion
}
